<?php	
	class API
	{
		/**
		*	Constructor de la clase.
		*	@autor: jsl.
		*	@date: 01 AGO 2011
		*/
		function API(){}
		
		/**
		*	Función responsable de listar las API.
		*	@autor: jsl.
		*	@date: 01 AGO 2011
		*/
		function getAPI(){
			$sql = "SELECT ap.api_code, ap.api_name FROM api ap;";			
			$result = mysql_query($sql);
			$data = array();
			$i=0;
			
			while($row = mysql_fetch_array($result)){
				$data[$i]->api_code = $row["api_code"];
				$data[$i]->api_name = $row["api_name"];			
				$i++;
			}
			return $data;
		}
		
		/**
		*	Funcion encargada de obtener la informacion de una API
		*	@autor: JSL
		*	@date: 07 DIC 2011
		*/
		function getInfoAPI($api_code){
			$sql = "SELECT ap.api_code, ap.api_name, ap.api_key, ap.api_url FROM api ap WHERE api_code=".(int) $api_code.";";
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result);
			
			$data->api_code = $row["api_code"];
			$data->api_name = $row["api_name"];
			$data->api_key = $row["api_key"];
			$data->api_url = $row["api_url"];
			
			return $data;
		}
		
		/**
		*	Funcion responsable de sincronizar y listar las marcas de un proveedor que coinciden con las de master-unlock en su nombre.
		*	@autor: jsl		
		*	@date: 01 AGO 2011
		*	@parameter->	$API_provider_code: es el codigo de proveedor a obtener las coincidencias en marcas con master-unlock.
		*/
		function syncBrandAPI($API_provider_code){
			$sql ="
			SELECT ab.API_brand_code, ab.API_provider_code, ab.MU_brand_code, ab.API_provider_brand_code, ab.API_provider_brand_name, b.bra_code, b.bra_name 
			FROM api_brand ab JOIN brand b
			ON ab.API_provider_brand_name=b.bra_name WHERE ab.API_provider_code=".$API_provider_code." AND ab.MU_brand_code=0;";
			$msg_error = "";
			
			$result = mysql_query($sql);
			$data = array();
			$cont = 0;//cantidad de marcas sincronizadas
			$i=0;			
			while($row = mysql_fetch_array($result)){
				$data[$i]->API_brand_code = $row["API_brand_code"];
				$data[$i]->API_provider_code = $row["API_provider_code"];
				$data[$i]->MU_brand_code = $row["MU_brand_code"];
				$data[$i]->API_provider_brand_code = $row["API_provider_brand_code"];
				$data[$i]->API_provider_brand_name = $row["API_provider_brand_name"];
				$data[$i]->bra_code = $row["bra_code"];
				$data[$i]->bra_name = $row["bra_name"];
				
				$data[$i]->new_MU_brand_code = $data[$i]->bra_code;
				$count_API_brand = $this->count_API_brand($data[$i]);
				if($count_API_brand==0){//validamos que la marca no este relacionada con otra del mismo proveedor (API).						
					if($this->update_API_brand($data[$i]))
						$cont++;
					else
						$msg_error .= "Error: NO es posible sincronizar la marca ".$data[$i]->bra_name." con c&oacute;digo ".$data[$i]->bra_code." de master-unlock.<br />";
				}
				else
					$msg_error .= "Error: La marca <strong>".$data[$i]->API_provider_brand_name."</strong> con c&oacute;digo <strong>".$data[$i]->API_provider_brand_code."</strong> en la API deber&iacute;a estar relacionada con la marca <strong>".$data[$i]->bra_name."</strong> con c&oacute;digo <strong>".$data[$i]->bra_code."</strong> en master-unlock.<br />";
				
				$i++;
			}			
			return array($data,$msg_error,$cont);
		}
		
		/**
		*	Función responsable de contar cuantas veces está relacionada una marca de master-unlock con una marca de un proveedor (API).
		*	@autor: jsl.		
		*	@date: 01 AGO 2011
		*/
		function count_API_brand($data){
			$sql = " 
			SELECT COUNT(ab.API_brand_code) AS cant 
			FROM api_brand ab 
			WHERE ab.MU_brand_code=".$data->new_MU_brand_code." AND ab.API_provider_code=".$data->API_provider_code.";";
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result);
			return $row["cant"];
		}
		
		/**
		*	Función responsable de asociar el codigo de la marca de master-unlock con la marca de una API.
		*	@autor: jsl.		
		*	@date: 01 AGO 2011
		*/
		function update_API_brand($data){
			$sql = "UPDATE api_brand SET MU_brand_code = '".$data->new_MU_brand_code."' WHERE API_brand_code=".$data->API_brand_code.";";
			return mysql_query($sql);
		}
		
		//------------------------------------------------------
				
		/**
		*	Funcion responsable de sincronizar y listar los mobile de un proveedor que coinciden con los de master-unlock en su nombre.
		*	@autor: jsl		
		*	@date: 02 AGO 2011
		*	@parameter->	$API_provider_code: es el codigo de proveedor a obtener las coincidencias en mobile con master-unlock.
		*/
		function syncMobileAPI($API_provider_code){			
			$sql ="
			SELECT t0.API_mobile_code, t0.API_provider_code, t0.MU_mobile_code, t0.API_provider_brand_code, t0.API_provider_mobile_code, t0.API_provider_mobile_name,
			t0.MU_brand_code, t1.bra_code, t1.mod_code, t1.mod_name FROM
			
			(SELECT am.API_mobile_code, am.API_provider_code, am.MU_mobile_code, am.API_provider_brand_code, am.API_provider_mobile_code, am.API_provider_mobile_name,
			ab.MU_brand_code FROM api_mobile am
			JOIN api_brand ab ON ab.API_provider_brand_code=am.API_provider_brand_code WHERE am.MU_mobile_code=0) t0			
			,			
			(SELECT b.bra_code, m.mod_code, m.mod_name FROM model m, brand b WHERE m.bra_code=b.bra_code) t1
			
			WHERE t0.MU_brand_code=t1.bra_code AND t0.API_provider_mobile_name=t1.mod_name;";
			
			$msg_error = "";
			
			$result = mysql_query($sql);
			$data = array();
			$cont = 0;//cantidad de modelos sincronizadas
			$i=0;
			while($row = mysql_fetch_array($result)){
				$data[$i]->API_mobile_code = $row["API_mobile_code"];
				$data[$i]->API_provider_code = $row["API_provider_code"];
				$data[$i]->MU_mobile_code = $row["MU_mobile_code"];
				$data[$i]->API_provider_brand_code = $row["API_provider_brand_code"];		
				$data[$i]->API_provider_mobile_code = $row["API_provider_mobile_code"];
				$data[$i]->API_provider_mobile_name = $row["API_provider_mobile_name"];
				$data[$i]->mod_code = $row["mod_code"];
				$data[$i]->mod_name = $row["mod_name"];
				
				$data[$i]->new_MU_mobile_code = $data[$i]->mod_code;
				$count_API_mobile = $this->count_API_mobile($data[$i]);
				if($count_API_mobile == 0){//validamos que el mobile  no este relacionada con otro mobile del mismo proveedor (API).						
					if($this->update_API_mobile($data[$i]))
						$cont++;
					else
						$msg_error .= "Error: NO es posible sincronizar el modelo ".$data[$i]->mod_name.".<br />";
				}
				else
					$msg_error .= "Error: La marca con codigo ".$data[$i]->mod_code." ya ha sido relacionada con otro mobile en la misma API.<br />";
				
				$i++;
			}			
			return array($data,$msg_error,$cont);
		}
		
		/**
		*	Función responsable de contar cuantas veces está relacionada un mobile de master-unlock con uno de un proveedor (API).
		*	@autor: jsl.		
		*	@date: 02 AGO 2011
		*/
		function count_API_mobile($data){
			$sql = "SELECT COUNT(am.API_mobile_code) AS cant FROM api_mobile am 
					WHERE am.MU_mobile_code=".$data->new_MU_mobile_code." AND am.API_provider_code=".$data->API_provider_code.";";
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result);
			return $row["cant"];
		}
		
		
		
		//------------------------------------------------------
		
		/**
		*	Funcion responsable de sincronizar y listar las marcas de un proveedor que coinciden con las de master-unlock en su nombre.
		*	@autor: jsl		
		*	@date: 01 AGO 2011
		*	@parameter->	$API_provider_code: es el codigo de proveedor a obtener las coincidencias en marcas con master-unlock.
		*/
		function syncCountryAPI($API_provider_code){
			$sql ="
			SELECT ac.API_country_code, ac.API_provider_code, ac.MU_country_code, ac.API_provider_country_code, c.cou_code, c.cou_name 
			FROM api_country ac JOIN country c
			ON ac.API_provider_country_name=c.cou_name WHERE ac.API_provider_code=".$API_provider_code." AND ac.MU_country_code=0;";
			
			$msg_error = "";
			
			$result = mysql_query($sql);
			$data = array();
			$cont = 0;//cantidad de paises sincronizadas
			$i=0;			
			while($row = mysql_fetch_array($result)){
				$data[$i]->API_country_code = $row["API_country_code"];
				$data[$i]->API_provider_code = $row["API_provider_code"];
				$data[$i]->MU_country_code = $row["MU_country_code"];
				$data[$i]->API_provider_country_code = $row["API_provider_country_code"];				
				$data[$i]->cou_code = $row["cou_code"];
				$data[$i]->cou_name = $row["cou_name"];
				
				$data[$i]->new_MU_country_code = $data[$i]->cou_code;
				$count_API_country = $this->count_API_country($data[$i]);
				if($count_API_country==0){//validamos que el pais NO este relacionado con otro del mismo proveedor (API).						
					if($this->update_API_country($data[$i]))
						$cont++;
					else
						$msg_error .= "Error: NO es posible sincronizar el pais ".$data[$i]->cou_name.".<br />";
				}
				else
					$msg_error .= "Error: El pais con codigo ".$data[$i]->cou_code." ya ha sido relacionado con otra pais en la misma API.<br />";
				
				$i++;
			}			
			return array($data,$msg_error,$cont);
		}
						
		//------------------------------------------------------
		
		/**
		*	Funcion responsable de sincronizar y listar las redes de un proveedor que coinciden con las de master-unlock en su nombre.
		*	@autor: jsl		
		*	@date: 02 AGO 2011
		*	@parameter->	$API_provider_code: es el codigo de proveedor a obtener las coincidencias en marcas con master-unlock.
		*/
		function syncNetworkAPI($API_provider_code){
			$sql ="			
			SELECT t0.API_network_code, t0.API_provider_code, t0.MU_network_code, t0.API_provider_country_code, t0.API_provider_network_code,
			t0.API_provider_network_name, t0.MU_country_code, t1.cou_code, t1.red_id, t1.red_name FROM
			
			(SELECT an.API_network_code, an.API_provider_code, an.MU_network_code, an.API_provider_country_code, an.API_provider_network_code,
			 an.API_provider_network_name, ac.MU_country_code FROM api_network an
			JOIN api_country ac ON ac.API_provider_country_code=an.API_provider_country_code WHERE an.MU_network_code=0) t0			
			,			
			(SELECT c.cou_code, r.red_id, r.red_name FROM red r, country c WHERE r.cou_code=c.cou_code) t1
			
			WHERE t0.MU_country_code=t1.cou_code AND t0.API_provider_network_name=t1.red_name;";
			
			
			$msg_error = "";
			
			$result = mysql_query($sql);
			$data = array();
			$cont = 0;//cantidad de redes sincronizadas
			$i=0;			
			while($row = mysql_fetch_array($result)){
				$data[$i]->API_network_code = $row["API_network_code"];
				$data[$i]->API_provider_code = $row["API_provider_code"];
				$data[$i]->MU_network_code = $row["MU_network_code"];
				$data[$i]->API_provider_network_code = $row["API_provider_network_code"];				
				$data[$i]->red_id = $row["red_id"];
				$data[$i]->red_name = $row["red_name"];
				
				$data[$i]->new_MU_network_code = $data[$i]->red_id;
				$count_API_network = $this->count_API_network($data[$i]);
				if($count_API_network==0){//validamos que la red NO este relacionado con otra red del mismo proveedor (API).						
					if($this->update_API_network($data[$i]))
						$cont++;
					else
						$msg_error .= "Error: NO es posible sincronizar la red ".$data[$i]->red_name.".<br />";
				}
				//else
					//$msg_error .= "Error: La red con codigo ".$data[$i]->red_id." de Master-Unlock ya ha sido relacionado con otra red en la misma API.<br />";
				
				$i++;
			}			
			return array($data,$msg_error,$cont);
		}
				
		//------------------------------------------------------
		
		/**
		*	Funcion responsable de sincronizar y listar los grupos de un proveedor que coinciden con los de master-unlock en su nombre.
		*	@autor: jsl		
		*	@date: 03 AGO 2011
		*	@parameter->	$API_provider_code: es el codigo de proveedor a obtener las coincidencias en marcas con master-unlock.
		*/
		function syncGroupAPI($API_provider_code){
			$sql ="
			SELECT ag.API_group_code, ag.API_provider_code, ag.MU_group_code, ag.API_provider_group_code, st.ser_type_code, st.ser_type_name 
			FROM api_group ag JOIN service_type st
			ON ag.API_provider_group_name=st.ser_type_name WHERE ag.API_provider_code=".$API_provider_code." AND ag.MU_group_code=0;";
			
			$msg_error = "";
			
			$result = mysql_query($sql);
			$data = array();
			$cont = 0;//cantidad de grupos sincronizados.
			$i=0;
			while($row = mysql_fetch_array($result)){
				$data[$i]->API_group_code = $row["API_group_code"];
				$data[$i]->API_provider_code = $row["API_provider_code"];
				$data[$i]->MU_group_code = $row["MU_group_code"];
				$data[$i]->API_provider_group_code = $row["API_provider_group_code"];
				$data[$i]->ser_type_code = $row["ser_type_code"];
				$data[$i]->ser_type_name = $row["ser_type_name"];
				
				$data[$i]->new_MU_group_code = $data[$i]->ser_type_code;
				$count_API_group = $this->count_API_group($data[$i]);
				if($count_API_group==0){//validamos que el grupo NO este relacionado con otro servicio del mismo proveedor (API).
					if($this->update_API_group($data[$i]))
						$cont++;
					else
						$msg_error .= "Error: NO es posible sincronizar el grupo ".$data[$i]->ser_type_name.".<br />";
				}
				else
					$msg_error .= "Error: El grupo con codigo ".$data[$i]->ser_type_code." ya ha sido relacionado con otro grupo en la misma API.<br />";
				
				$i++;
			}			
			return array($data,$msg_error,$cont);
		}
		
		/**
		*	Función responsable de contar cuantas veces está relacionado un grupo de master-unlock con uno de un proveedor (API).
		*	@autor: jsl.
		*	@date: 03 AGO 2011
		*/
		function count_API_group($data){
			$sql = " 
			SELECT COUNT(ag.API_group_code) AS cant
			FROM api_group ag
			WHERE ag.MU_group_code=".$data->new_MU_group_code." AND ag.API_provider_code=".$data->API_provider_code.";";
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result);
			return $row["cant"];
		}
		
		/**
		*	Función responsable de asociar el codigo de un grupo de master-unlock con uno de la API.
		*	@autor: jsl.
		*	@date: 03 AGO 2011
		*/
		function update_API_group($data){
			$sql = "
			UPDATE api_group SET MU_group_code = '".$data->new_MU_group_code."' 
			WHERE API_group_code=".$data->API_group_code.";";
			return mysql_query($sql);
		}
		
		//------------------------------------------------------
			
		/**
		*	Funcion responsable de sincronizar y listar los servicios de un proveedor que coinciden con los de master-unlock en su nombre.
		*	@autor: jsl		
		*	@date: 03 AGO 2011
		*	@parameter->	$API_provider_code: es el codigo de proveedor a obtener las coincidencias en marcas con master-unlock.
		*/
		function syncToolAPI($API_provider_code){
			$sql ="
			SELECT t0.API_tool_code, t0.API_provider_code, t0.MU_tool_code, t0.API_provider_group_code, t0.API_provider_tool_code, t0.API_provider_tool_name,
			t0.MU_group_code, t1.ser_type_code, t1.ser_code, t1.ser_name FROM
			
			(SELECT at.API_tool_code, at.API_provider_code, at.MU_tool_code, at.API_provider_group_code, at.API_provider_tool_code, at.API_provider_tool_name,
			ag.MU_group_code FROM api_tool at
			JOIN api_group ag ON ag.API_provider_group_code=at.API_provider_group_code WHERE at.MU_tool_code=0) t0			
			,			
			(SELECT st.ser_type_code, s.ser_code, s.ser_name FROM services s, service_type st WHERE s.ser_type_code=st.ser_type_code) t1
			
			WHERE t0.MU_group_code=t1.ser_type_code AND t0.API_provider_tool_name=t1.ser_name;";
			
			$msg_error = "";
			
			$result = mysql_query($sql);
			$data = array();
			$cont = 0;//cantidad de servicios sincronizados.
			$i=0;			
			while($row = mysql_fetch_array($result)){
				$data[$i]->API_tool_code = $row["API_tool_code"];
				$data[$i]->API_provider_code = $row["API_provider_code"];
				$data[$i]->MU_tool_code = $row["MU_tool_code"];
				$data[$i]->API_provider_tool_code = $row["API_provider_tool_code"];				
				$data[$i]->ser_code = $row["ser_code"];
				$data[$i]->ser_name = $row["ser_name"];
				
				$data[$i]->new_MU_tool_code = $data[$i]->ser_code;
				$count_API_tool = $this->count_API_tool($data[$i]);
				if($count_API_tool==0){//validamos que el servicio NO este relacionado con otro servicio del mismo proveedor (API).						
					if($this->update_API_tool($data[$i]))
						$cont++;
					else
						$msg_error .= "Error: NO es posible sincronizar el servicio ".$data[$i]->ser_name.".<br />";
				}
				else
					$msg_error .= "Error: El servicio con codigo ".$data[$i]->ser_code." ya ha sido relacionado con otro servicio en la misma API.<br />";
				
				$i++;
			}			
			return array($data,$msg_error,$cont);
		}
		
		/**
		*	Función responsable de verificar si un servicio de unlock base ya esta en la bd de master
		*	@autor: dmhg.
		*	@date: 31/10/2012
		*/
		function existeServicio($id){
			$sql = " SELECT COUNT(API_tool_code) AS cant FROM api_tool WHERE API_provider_tool_code=".$id;
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result);
			return $row["cant"];
		}
		
		/**
		*	Esta función inserta un servicio nuevo de unlock base.
		*	@autor: DMHG
		*	@date: 31/10/2012
		*/
		function sincronizaServicio($datos){
			$sql = "INSERT INTO api_tool (API_tool_code, API_provider_code,	MU_tool_code, API_provider_group_code, API_provider_tool_code, API_provider_tool_name) VALUES (NULL, '".$datos->api."', '', '".$datos->group."', '".$datos->id."', '".$datos->name."')";
			if ( $result = mysql_query( $sql ) )
				return mysql_insert_id();
			else
				return false;
		}
		
		/**
		*	Función responsable de verificar si un servicio de unlock base ya esta en la bd de master
		*	@autor: dmhg.
		*	@date: 31/10/2012
		*/
		function existeGrupo($id){
			$sql = " SELECT COUNT(API_group_code) AS cant FROM api_group WHERE API_provider_group_code=".$id;
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result);
			return $row["cant"];
		}
		
		/**
		*	Esta función inserta un servicio nuevo de unlock base.
		*	@autor: DMHG
		*	@date: 31/10/2012
		*/
		function sincronizaGrupo($datos){
			$sql = "INSERT INTO api_group (API_group_code, API_provider_code, MU_group_code, API_provider_group_code, API_provider_group_name) VALUES (NULL, '".$datos->api."', '', '".$datos->group."', '".$datos->groupName."')";
			if ( $result = mysql_query( $sql ) )
				return mysql_insert_id();
			else
				return false;
		}
		
		/**
		*	Función responsable de contar cuantas veces está relacionado un servicio de master-unlock con uno de un proveedor (API).
		*	@autor: jsl.
		*	@date: 03 AGO 2011
		*/
		function count_API_tool($data){
			$sql = " 
			SELECT COUNT(at.API_tool_code) AS cant
			FROM api_tool at
			WHERE at.MU_tool_code=".$data->new_MU_tool_code." AND at.API_provider_code=".$data->API_provider_code.";";
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result);
			return $row["cant"];
		}
		
		/**
		*	Función responsable de asociar el codigo de un servicio de master-unlock con uno de la API.
		*	@autor: jsl.
		*	@date: 03 AGO 2011
		*/
		function update_API_tool($data){
			$sql = "UPDATE api_tool SET MU_tool_code = '".$data->new_MU_tool_code."' WHERE API_tool_code=".$data->API_tool_code.";";
			return mysql_query($sql);
		}
		
		//-----------------------
		
		/**
		*	Funcion responsable de listar las marcas de una API relacionadas con las marcas de master-unlock.
		*	@autor: jsl
		*	@date: 03 AGO 2011
		*	@parameter->	$API_provider_code: es el codigo de la API.
		*/
		function listBrandAPI($API_provider_code,$init=0,$amount=0,$search='',$order_by=''){
			$where = '';
			if($search != '')
				$where = " AND (API_provider_brand_name like '%".$search."%' OR bra_name like '%".$search."%') ";
			
			$sql ="
			SELECT ab.API_brand_code, ab.API_provider_code, ab.MU_brand_code, ab.API_provider_brand_code, ab.API_provider_brand_name, b.bra_code, b.bra_name 
			FROM api_brand ab LEFT JOIN brand b
			ON ab.MU_brand_code=b.bra_code WHERE ab.API_provider_code=".$API_provider_code.$where.$order_by;
			
			if(!($init==0 && $amount==0))
				$sql .= " LIMIT ".$init.",".$amount.";";
			
			$result = mysql_query($sql);
			$data = array();			
			$i=0;			
			while($row = mysql_fetch_array($result)){
				$data[$i]->API_brand_code = $row["API_brand_code"];
				$data[$i]->API_provider_code = $row["API_provider_code"];
				$data[$i]->MU_brand_code = $row["MU_brand_code"];
				$data[$i]->API_provider_brand_code = $row["API_provider_brand_code"];
				$data[$i]->API_provider_brand_name = $row["API_provider_brand_name"];
				$data[$i]->bra_code = $row["bra_code"];
				$data[$i]->bra_name = $row["bra_name"];
				
				$i++;
			}			
			return $data;
		}
		
		/**
		*	Funcion responsable de contar las marcas de una API relacionadas con las marcas de master-unlock en caso de que exista dicha relcaión.
		*	@autor: jsl
		*	@date: 05 AGO 2011
		*	@parameter->	$API_provider_code: es el codigo de la API.
		*/
		function countBrandAPI($API_provider_code,$search=''){
			$where = '';
			if($search != '')
				$where = " AND (API_provider_brand_name like '%".$search."%' OR bra_name like '%".$search."%') ";
			
			$sql ="
			SELECT COUNT(ab.API_brand_code) AS cant
			FROM api_brand ab LEFT JOIN brand b
			ON ab.MU_brand_code=b.bra_code WHERE ab.API_provider_code=".$API_provider_code.$where;
			
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result);
			return $row["cant"];
		}
		
		/**
		*	Esta funcion es la encargada de listar las marcas de master-unlock con su relación en una API si es que existe dicha relación.
		* 	@autor: jsl
		* 	@date: 19 AGO 2011
		*/
		public function listBrandMU($API_provider_code){
			$sql = "
			SELECT t0.bra_code, t0.bra_name, IFNULL(t1.API_brand_code,0) AS API_brand_code FROM
			(SELECT b.bra_code, b.bra_name FROM brand b) t0
			LEFT JOIN (SELECT ab.API_brand_code, ab.MU_brand_code FROM api_brand ab WHERE ab.API_provider_code = ".$API_provider_code.") t1
			ON t0.bra_code=t1.MU_brand_code;";
			
			$result = mysql_query($sql);		
			$data = array();	
			$i=0;
			while($row=mysql_fetch_array($result)){
				$data[$i]->API_brand_code = $row['API_brand_code'];
				$data[$i]->bra_code = $row['bra_code'];
				$data[$i]->bra_name = $row['bra_name'];
				$i++;
			}			
			return $data;
		}
		
		
		/**
		*	Funcion responsable de listar los mobile de una API relacionadas con los mobile de master-unlock.
		*	@parameter->	$API_provider_code: es el código de proveedor del cuál se esta relacionando la información.
		*	@autor: jsl
		*	@date: 17 AGO 2011
		*/
		function listMobileAPI($API_provider_code,$init=0,$amount=0,$search='',$order_by=''){
			$where = '';
			if($search != '')
				$where = " WHERE (bra_name like '%".$search."%' OR
								  API_provider_brand_name like '%".$search."%' OR
								  API_provider_mobile_name like '%".$search."%' OR
								  mod_name like '%".$search."%') ";
			
			$sql ="
			SELECT t2.API_mobile_code, t2.API_provider_code, t2.MU_mobile_code, t2.API_provider_brand_code, t2.API_provider_mobile_code, t2.API_provider_mobile_name, 
			t2.mod_code, t2.mod_name, t2.API_provider_brand_name, b.bra_name, b.bra_code FROM
			
			(SELECT t.API_mobile_code, t.API_provider_code, t.MU_mobile_code, t.API_provider_brand_code, t.API_provider_mobile_code, t.API_provider_mobile_name, 
			t.mod_code, t.mod_name, ab.API_provider_brand_name, ab.MU_brand_code FROM
			(SELECT am.API_mobile_code, am.API_provider_code, am.MU_mobile_code, am.API_provider_brand_code, am.API_provider_mobile_code, am.API_provider_mobile_name,
			 m.mod_code, m.mod_name 
			FROM api_mobile am LEFT JOIN model m
			ON am.MU_mobile_code=m.mod_code WHERE am.API_provider_code=".$API_provider_code.") t			
			JOIN api_brand ab ON t.API_provider_brand_code=ab.API_provider_brand_code WHERE ab.API_provider_code=".$API_provider_code.") t2
			
			LEFT JOIN brand b ON t2.MU_brand_code=b.bra_code ".$where.$order_by;
			
			if(!($init==0 && $amount==0))
				$sql .= " LIMIT ".$init.",".$amount.";";
			
			$result = mysql_query($sql);
			$data = array();			
			$i=0;			
			while($row = mysql_fetch_array($result)){
				$data[$i]->API_provider_code = $row["API_provider_code"];
				
				$data[$i]->API_provider_brand_code = $row["API_provider_brand_code"];
				$data[$i]->API_provider_brand_name = $row["API_provider_brand_name"];
				
				$data[$i]->bra_code = $row["bra_code"];
				$data[$i]->bra_name = $row["bra_name"];
				
				$data[$i]->API_mobile_code = $row["API_mobile_code"];
				$data[$i]->API_provider_code = $row["API_provider_code"];
				$data[$i]->MU_mobile_code = $row["MU_mobile_code"];
				$data[$i]->API_provider_mobile_code = $row["API_provider_mobile_code"];
				$data[$i]->API_provider_mobile_name = $row["API_provider_mobile_name"];
				$data[$i]->mod_code = $row["mod_code"];
				$data[$i]->mod_name = $row["mod_name"];

				if($data[$i]->bra_code > 0)
					$data[$i]->listMobileMU = $this->listMobileMU($data[$i]->API_provider_code,$data[$i]->bra_code);
				else
					$data[$i]->listMobileMU = NULL;
				
				$i++;
			}			
			return $data;
		}		
		
		/**
		*	Funcion responsable de contar los mobile de una API relacionadas con los mobile de master-unlock.
		*	@parameter->	$API_provider_code: es el código de proveedor del cuál se esta relacionando la información.
		*	@autor: jsl
		*	@date: 17 AGO 2011
		*/
		function countMobileAPI($API_provider_code,$search=''){
			$where = '';
			if($search != '')
				$where = " WHERE (bra_name like '%".$search."%' OR
								  API_provider_brand_name like '%".$search."%' OR
								  API_provider_mobile_name like '%".$search."%' OR
								  mod_name like '%".$search."%') ";
			
			$sql ="
			SELECT COUNT(*) AS cant FROM
			
			(SELECT t.API_mobile_code, t.API_provider_code, t.MU_mobile_code, t.API_provider_brand_code, t.API_provider_mobile_code, t.API_provider_mobile_name, 
			t.mod_code, t.mod_name, ab.API_provider_brand_name, ab.MU_brand_code FROM
			(SELECT am.API_mobile_code, am.API_provider_code, am.MU_mobile_code, am.API_provider_brand_code, am.API_provider_mobile_code, am.API_provider_mobile_name,
			 m.mod_code, m.mod_name 
			FROM api_mobile am LEFT JOIN model m
			ON am.MU_mobile_code=m.mod_code WHERE am.API_provider_code=".$API_provider_code.") t			
			JOIN api_brand ab ON t.API_provider_brand_code=ab.API_provider_brand_code WHERE ab.API_provider_code=".$API_provider_code.") t2
			
			LEFT JOIN brand b ON t2.MU_brand_code=b.bra_code ".$where;
			
			$result = mysql_query($sql);			
			$row = mysql_fetch_array($result);
			return $row["cant"];
		}
		
		/**
		*	Esta funcion es la encargada de listar los modelos de master-unlock con su relación en una API si es que existe dicha relación.
		*	@autor: jsl
		*	@date: 04 AGO 2011
		*/
		public function listMobileMU($API_provider_code,$bra_code){
			$where = "";
			if($bra_code > 0)
				$where = " WHERE m.bra_code=".$bra_code;
			$sql = "
			SELECT t0.mod_code, t0.mod_name, IFNULL(t1.API_mobile_code,0) AS API_mobile_code FROM
			(SELECT m.mod_code, m.mod_name FROM model m ".$where.") t0
			LEFT JOIN (SELECT am.API_mobile_code, am.MU_mobile_code FROM api_mobile am WHERE API_provider_code = ".$API_provider_code.") t1
			ON t0.mod_code=t1.MU_mobile_code;";
			
			$result = mysql_query($sql);		
			$data = array();	
			$i=0;
			while($row=mysql_fetch_array($result)){
				$data[$i]->API_mobile_code = $row['API_mobile_code'];//si este valor es cero, significa que NO tiene relación con un modelo en la API especificada.
				$data[$i]->mod_code = $row['mod_code'];
				$data[$i]->mod_name = $row['mod_name'];
				$i++;
			}			
			return $data;
		}
		
		
		/**
		*	Funcion responsable de listar los paises de una API relacionados con los paises de master-unlock.
		*	@autor: jsl
		*	@date: 19 AGO 2011
		*	@parameter->	$API_provider_code: es el codigo de la API.
		*/
		function listCountryAPI($API_provider_code,$init=0,$amount=0,$search='',$order_by=''){
			$where = '';
			if($search != '')
				$where = " AND (API_provider_country_name like '%".$search."%' OR cou_name like '%".$search."%') ";
			
			$sql ="
			SELECT ac.API_country_code, ac.API_country_code, ac.MU_country_code, ac.API_provider_country_code, ac.API_provider_country_name, c.cou_code, c.cou_name 
			FROM api_country ac LEFT JOIN country c
			ON ac.MU_country_code=c.cou_code WHERE ac.API_provider_code=".$API_provider_code.$where.$order_by;
			
			if(!($init==0 && $amount==0))
				$sql .= " LIMIT ".$init.",".$amount.";";
			
			$result = mysql_query($sql);
			$data = array();			
			$i=0;			
			while($row = mysql_fetch_array($result)){
				$data[$i]->API_country_code = $row["API_country_code"];
				$data[$i]->API_provider_code = $row["API_provider_code"];
				$data[$i]->MU_country_code = $row["MU_country_code"];
				$data[$i]->API_provider_country_code = $row["API_provider_country_code"];
				$data[$i]->API_provider_country_name = $row["API_provider_country_name"];
				$data[$i]->cou_code = $row["cou_code"];
				$data[$i]->cou_name = $row["cou_name"];
				
				$i++;
			}			
			return $data;
		}
		
		/**
		*	Funcion responsable de contar los paises de una API relacionados con los paises de master-unlock.
		*	@autor: jsl
		*	@date: 19 AGO 2011
		*	@parameter->	$API_provider_code: es el codigo de la API.
		*/
		function countCountryAPI($API_provider_code,$search=''){
			$where = '';
			if($search != '')
				$where = " AND (API_provider_country_name like '%".$search."%' OR cou_name like '%".$search."%') ";
			
			$sql ="
			SELECT COUNT(ac.API_country_code) AS cant
			FROM api_country ac LEFT JOIN country c
			ON ac.MU_country_code=c.cou_code WHERE ac.API_provider_code=".$API_provider_code.$where;
						
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result);
			return $row["cant"];
		}
		
		/**
		*	Esta función es la encargada de listar los paises de master-unlock con su relación en una API si es que existe dicha relación.
		*	@autor: jsl.
		*	@date: 19 AGO 2011
		*/
		public function listCountryMU($API_provider_code){
			$sql = "
			SELECT t0.cou_code, t0.cou_name, IFNULL(t1.API_country_code,0) AS API_country_code FROM
			(SELECT c.cou_code, c.cou_name FROM country c) t0
			LEFT JOIN (SELECT ac.API_country_code, ac.MU_country_code FROM api_country ac WHERE ac.API_provider_code = ".$API_provider_code.") t1
			ON t0.cou_code=t1.MU_country_code;";
			$result = mysql_query($sql);		
			$data = array();	
			$i=0;
			while($row=mysql_fetch_array($result)){
				$data[$i]->API_country_code = $row['API_country_code'];
				$data[$i]->cou_code = $row['cou_code'];
				$data[$i]->cou_name = $row['cou_name'];
				$i++;
			}			
			return $data;
		}
		
		
		/**
		*	Funcion responsable de listar los network de una API relacionadas con los mobile de master-unlock.
		*	@parameter->	$API_provider_code: es el código de proveedor del cuál se esta relacionando la información.
		*	@autor: jsl.
		*	@date: 19 AGO 2011.
		*/
		function listNetworkAPI($API_provider_code,$init=0,$amount=0,$search='',$order_by=''){
			$where = '';
			if($search != '')
				$where = " WHERE (cou_name like '%".$search."%' OR
								  API_provider_country_name like '%".$search."%' OR
								  API_provider_network_name like '%".$search."%' OR
								  red_name like '%".$search."%') ";
			
			$sql ="
			SELECT t2.API_network_code, t2.API_provider_code, t2.MU_network_code, t2.API_provider_country_code, t2.API_provider_network_code,
			t2.API_provider_network_name, t2.red_id, t2.red_name, t2.API_provider_country_name, c.cou_name, c.cou_code FROM
			
			(SELECT t.API_network_code, t.API_provider_code, t.MU_network_code, t.API_provider_country_code, t.API_provider_network_code, t.API_provider_network_name, 
			t.red_id, t.red_name, ac.API_provider_country_name, ac.MU_country_code FROM
			
			(SELECT an.API_network_code, an.API_provider_code, an.MU_network_code, an.API_provider_country_code, an.API_provider_network_code,
				an.API_provider_network_name, r.red_id, r.red_name
				FROM api_network an LEFT JOIN red r
				ON an.MU_network_code=r.red_id WHERE an.API_provider_code=".$API_provider_code.") t
						
			JOIN api_country ac ON t.API_provider_country_code=ac.API_provider_country_code WHERE ac.API_provider_code=".$API_provider_code.") t2
						
			LEFT JOIN country c ON t2.MU_country_code=c.cou_code ".$where.$order_by;
			//die('<pre>'.$sql.'</pre>');
			if(!($init==0 && $amount==0))
				$sql .= " LIMIT ".$init.",".$amount.";";
			
			$result = mysql_query($sql);
			$data = array();			
			$i=0;			
			while($row = mysql_fetch_array($result)){
				$data[$i]->API_provider_code = $row["API_provider_code"];
				
				$data[$i]->API_provider_country_code = $row["API_provider_country_code"];
				$data[$i]->API_provider_country_name = $row["API_provider_country_name"];
				
				$data[$i]->cou_code = $row["cou_code"];
				$data[$i]->cou_name = $row["cou_name"];
				
				$data[$i]->API_network_code = $row["API_network_code"];
				$data[$i]->API_provider_code = $row["API_provider_code"];
				$data[$i]->MU_network_code = $row["MU_network_code"];
				$data[$i]->API_provider_network_code = $row["API_provider_network_code"];
				$data[$i]->API_provider_network_name = $row["API_provider_network_name"];
				$data[$i]->red_id = $row["red_id"];
				$data[$i]->red_name = $row["red_name"];

				if($data[$i]->cou_code > 0)
					$data[$i]->listNetworkMU = $this->listNetworkMU($data[$i]->API_provider_code,$data[$i]->cou_code);
				else
					$data[$i]->listNetworkMU = NULL;
				
				$i++;
			}			
			return $data;
		}		
		
		/**
		*	Funcion responsable de contar los network de una API relacionadas con los network de master-unlock.
		*	@parameter->	$API_provider_code: es el código de proveedor del cuál se esta relacionando la información.
		*	@autor: jsl.
		*	@date: 19 AGO 2011.
		*/
		function countNetworkAPI($API_provider_code,$search=''){
			$where = '';
			if($search != '')
				$where = " WHERE (API_provider_country_name like '%".$search."%' OR API_provider_network_name like '%".$search."%' OR red_name like '%".$search."%') ";
			
			$sql ="
			SELECT COUNT(*) AS cant FROM
			
			(SELECT t.API_network_code, t.API_provider_code, t.MU_network_code, t.API_provider_country_code, t.API_provider_network_code, t.API_provider_network_name, 
			t.red_id, t.red_name, ac.API_provider_country_name, ac.MU_country_code FROM
			
			(SELECT an.API_network_code, an.API_provider_code, an.MU_network_code, an.API_provider_country_code, an.API_provider_network_code,
				an.API_provider_network_name, r.red_id, r.red_name
				FROM api_network an LEFT JOIN red r
				ON an.MU_network_code=r.red_id WHERE an.API_provider_code=".$API_provider_code.") t
						
			JOIN api_country ac ON t.API_provider_country_code=ac.API_provider_country_code WHERE ac.API_provider_code=".$API_provider_code.") t2
						
			LEFT JOIN country c ON t2.MU_country_code=c.cou_code ".$where;
			
			$result = mysql_query($sql);			
			$row = mysql_fetch_array($result);
			return $row["cant"];
		}
		
		/**
		*	Esta función es la encargada de listar los network de master-unlock con su relación en una API si es que existe dicha relación.
		*	@autor: jsl.
		*	@date: 19 AGO 2011.
		*/
		public function listNetworkMU($API_provider_code,$cou_code){
			$where = "";
			if($cou_code > 0)
				$where = " WHERE r.cou_code=".$cou_code;
			
			$sql = "
			SELECT t0.red_id, t0.red_name, IFNULL(t1.API_network_code,0) AS API_network_code FROM
			(SELECT r.red_id, r.red_name FROM red r ".$where.") t0
			LEFT JOIN (SELECT an.API_network_code, an.MU_network_code FROM api_network an WHERE API_provider_code = ".$API_provider_code.") t1
			ON t0.red_id=t1.MU_network_code;";
			
			$result = mysql_query($sql);		
			$data = array();	
			$i=0;
			while($row=mysql_fetch_array($result)){
				$data[$i]->API_network_code = $row['API_network_code'];
				$data[$i]->red_id = $row['red_id'];
				$data[$i]->red_name = $row['red_name'];
				$i++;
			}			
			return $data;
		}
		
		
		/**
		*	Funcion responsable de listar los grupos de una API relacionados con los grupos de master-unlock.
		*	@autor: jsl.
		*	@date: 19 AGO 2011
		*	@parameter->	$API_provider_code: es el codigo de la API.
		*/
		function listGroupAPI($API_provider_code,$init=0,$amount=0,$search='',$order_by=''){
			$where = '';
			if($search != '')
				$where = " AND (API_provider_group_name like '%".$search."%' OR ser_type_name like '%".$search."%') ";
			
			$sql ="
			SELECT ag.API_group_code, ag.API_group_code, ag.MU_group_code, ag.API_provider_group_code, ag.API_provider_group_name, st.ser_type_code, st.ser_type_name 
			FROM api_group ag LEFT JOIN service_type st
			ON ag.MU_group_code=st.ser_type_code WHERE ag.API_provider_code=".$API_provider_code.$where.$order_by;
			
			if(!($init==0 && $amount==0))
				$sql .= " LIMIT ".$init.",".$amount.";";
			
			$result = mysql_query($sql);
			$data = array();			
			$i=0;			
			while($row = mysql_fetch_array($result)){
				$data[$i]->API_group_code = $row["API_group_code"];
				$data[$i]->API_provider_code = $row["API_provider_code"];
				$data[$i]->MU_group_code = $row["MU_group_code"];
				$data[$i]->API_provider_group_code = $row["API_provider_group_code"];
				$data[$i]->API_provider_group_name = $row["API_provider_group_name"];
				$data[$i]->ser_type_code = $row["ser_type_code"];
				$data[$i]->ser_type_name = $row["ser_type_name"];
				
				$i++;
			}			
			return $data;
		}
		
		/**
		*	Funcion responsable de contar los grupos de una API relacionados con los grupos de master-unlock.
		*	@autor: jsl.
		*	@date: 19 AGO 2011.
		*	@parameter->	$API_provider_code: es el codigo de la API.
		*/
		function countGroupAPI($API_provider_code,$search=''){
			$where = '';
			if($search != '')
				$where = " AND (API_provider_group_name like '%".$search."%' OR ser_type_name like '%".$search."%') ";
			
			$sql ="
			SELECT COUNT(ag.API_group_code) AS cant
			FROM api_group ag LEFT JOIN service_type st
			ON ag.MU_group_code=st.ser_type_code WHERE ag.API_provider_code=".$API_provider_code.$where;
						
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result);
			return $row["cant"];
		}
		
		/**
		*	Esta función es la encargada de listar los grupos de master-unlock con su relación en una API si es que existe dicha relación.
		*	@autor: jsl.
		*	@date: 19 AGO 2011.
		*/
		public function listGroupMU($API_provider_code){
			$sql = "
			SELECT t0.ser_type_code, t0.ser_type_name, IFNULL(t1.API_group_code,0) AS API_group_code FROM
			(SELECT st.ser_type_code, st.ser_type_name FROM service_type st) t0
			LEFT JOIN (SELECT ag.API_group_code, ag.MU_group_code FROM api_group ag WHERE ag.API_provider_code = ".$API_provider_code.") t1
			ON t0.ser_type_code=t1.MU_group_code;";
			$result = mysql_query($sql);		
			$data = array();	
			$i=0;
			while($row=mysql_fetch_array($result)){
				$data[$i]->API_group_code = $row['API_group_code'];
				$data[$i]->ser_type_code = $row['ser_type_code'];
				$data[$i]->ser_type_name = $row['ser_type_name'];
				$i++;
			}			
			return $data;
		}
		
		
		/**
		*	Funcion responsable de listar los servicios de una API relacionadas con los servicios de master-unlock.
		*	@parameter->	$API_provider_code: es el código de proveedor del cuál se esta relacionando la información.
		*	@autor: jsl
		*	@date: 19 AGO 2011
		*/
		function listToolAPI($API_provider_code,$init=0,$amount=0,$search='',$order_by=''){
			$where = '';
			if($search != '')
				$where = " WHERE (ser_type_name like '%".$search."%' OR
								  API_provider_group_name like '%".$search."%' OR
								  API_provider_tool_name like '%".$search."%' OR
								  ser_name like '%".$search."%') ";
			
			$sql ="
			SELECT t2.API_tool_code, t2.API_provider_code, t2.MU_tool_code, t2.API_provider_group_code, t2.API_provider_tool_code, t2.API_provider_tool_name, 
			t2.ser_code, t2.ser_name, t2.API_provider_group_name, st.ser_type_name, st.ser_type_code FROM
			
			(SELECT t.API_tool_code, t.API_provider_code, t.MU_tool_code, t.API_provider_group_code, t.API_provider_tool_code, t.API_provider_tool_name, 
			t.ser_code, t.ser_name, ag.API_provider_group_name, ag.MU_group_code FROM
			(SELECT at.API_tool_code, at.API_provider_code, at.MU_tool_code, at.API_provider_group_code, at.API_provider_tool_code, at.API_provider_tool_name,
			 s.ser_code, s.ser_name 
			FROM api_tool at LEFT JOIN services s
			ON at.MU_tool_code=s.ser_code WHERE at.API_provider_code=".$API_provider_code.") t			
			JOIN api_group ag ON t.API_provider_group_code=ag.API_provider_group_code) t2
			
			LEFT JOIN service_type st ON t2.MU_group_code=st.ser_type_code ".$where.$order_by;
			
			if(!($init==0 && $amount==0))
				$sql .= " LIMIT ".$init.",".$amount.";";
			
			$result = mysql_query($sql);
			$data = array();			
			$i=0;			
			while($row = mysql_fetch_array($result)){
				$data[$i]->API_provider_code = $row["API_provider_code"];
				
				$data[$i]->API_provider_group_code = $row["API_provider_group_code"];
				$data[$i]->API_provider_group_name = $row["API_provider_group_name"];
				
				$data[$i]->ser_type_code = $row["ser_type_code"];
				$data[$i]->ser_type_name = $row["ser_type_name"];
				
				$data[$i]->API_tool_code = $row["API_tool_code"];
				$data[$i]->API_provider_code = $row["API_provider_code"];
				$data[$i]->MU_tool_code = $row["MU_tool_code"];
				$data[$i]->API_provider_tool_code = $row["API_provider_tool_code"];
				$data[$i]->API_provider_tool_name = $row["API_provider_tool_name"];
				$data[$i]->ser_code = $row["ser_code"];
				$data[$i]->ser_name = $row["ser_name"];
								
				$data[$i]->listToolMU = $this->listToolMU($data[$i]->API_provider_code);
				
				$i++;
			}			
			return $data;
		}		
		
		/**
		*	Funcion responsable de contar los servicios de una API relacionadas con los servicios de master-unlock.
		*	@parameter->	$API_provider_code: es el código de proveedor del cuál se esta relacionando la información.
		*	@autor: jsl
		*	@date: 19 AGO 2011
		*/
		function countToolAPI($API_provider_code,$search=''){
			$where = '';
			if($search != '')
				$where = " WHERE (API_provider_group_name like '%".$search."%' OR API_provider_tool_name like '%".$search."%' OR ser_name like '%".$search."%') ";
			
			$sql ="
			SELECT COUNT(t1.API_tool_code) AS cant FROM
			(SELECT t.API_tool_code FROM			
			(SELECT at.API_tool_code, at.API_provider_group_code, at.API_provider_tool_name, s.ser_name FROM api_tool at LEFT JOIN services s
			ON at.MU_tool_code=s.ser_code WHERE at.API_provider_code=".$API_provider_code.") t			
			JOIN api_group ag ON t.API_provider_group_code=ag.API_provider_group_code ".$where.") t1";
			
			$result = mysql_query($sql);			
			$row = mysql_fetch_array($result);
			return $row["cant"];
		}
		
		/**
		*	Esta funcion es la encargada de listar los servicios de master-unlock con su relación en una API si es que existe dicha relación.
		*	@autor: jsl
		*	@date: 19 AGO 2011
		*/
		public function listToolMU($API_provider_code,$ser_type_code = 0){
			$where = "";
			if($ser_type_code > 0)
				$where = " WHERE s.ser_type_code=".$ser_type_code;
			$sql = "
			SELECT st.ser_type_name, t1.ser_code, t1.ser_name, t1.ser_type_code, t1.API_tool_code FROM
			service_type st JOIN
			(SELECT t0.ser_code, t0.ser_name, t0.ser_type_code, IFNULL(at.API_tool_code,0) AS API_tool_code FROM
			(SELECT s.ser_code, s.ser_name, s.ser_type_code FROM services s ".$where.") t0
			LEFT JOIN api_tool at
			ON t0.ser_code=at.MU_tool_code ORDER BY t0.ser_type_code) t1
			ON st.ser_type_code = t1.ser_type_code;";

			$result = mysql_query($sql);		
			$data = array();	
			$i=0;
			while($row=mysql_fetch_array($result)){
				$data[$i]->ser_type_name = $row['ser_type_name'];				
				$data[$i]->ser_code = $row['ser_code'];
				$data[$i]->ser_name = $row['ser_name'];
				$data[$i]->ser_type_code = $row['ser_type_code'];
				$data[$i]->API_tool_code = $row['API_tool_code'];
				$i++;
			}			
			return $data;
		}
		//------------------------------------------------------------------------------------------------------
		
		/**
		 *	Esta función se encarga de obtener todos los servicios Master-unlock,
		 *	tambien obtendrá sólo los servicios de Mster-Unlock asociados a un grupo en una API en caso de que el cod. de la API llegue por parámetro.
		 *	@parameter: $service_type	-->	tipo de servicio del cual necesitamos los servicios.
		 *				$API_provider_code	--> código de la API.
		 *	@autor: jsl.
		 *	@date: 12 AGO 2011
		 */
		public function listServiceByServiceType($type_service='',$API_provider_code=0){
			$where = "";
			if($API_provider_code > 0)
				$where = " WHERE at.API_provider_code=".$API_provider_code." ";
			$sql = "
			SELECT at.API_tool_code, IFNULL(at.API_provider_code,0) AS API_provider_code, IFNULL(at.API_provider_tool_code,0) AS API_provider_tool_code,
			t0.ser_type_code, t0.ser_type_name,	t0.ser_type_name_e,	t0.ser_code, t0.ser_name, t0.ser_name_e, t0.ser_price, t0.ser_price_us, t0.ser_price_bol 
			FROM
			(SELECT st.ser_type_code, st.ser_type_name, st.ser_type_name_e, st.ser_type_order, s.ser_code, s.ser_name, s.ser_name_e, s.ser_price, s.ser_price_us, 
			s.ser_price_bol, s.ser_order
			FROM service_type st JOIN services s ON st.ser_type_code=s.ser_type_code WHERE st.type_service='".$type_service."' AND s.ser_status='1') t0
			LEFT JOIN api_tool at 
			ON at.MU_tool_code=t0.ser_code ".$where." ORDER BY t0.ser_type_order, t0.ser_order, t0.ser_name;";
			$data = array();
			$result = mysql_query($sql);
			$i=0;
			while($row=mysql_fetch_array($result)){
				$data[$i]->API_tool_code = $row["API_tool_code"];
				$data[$i]->API_provider_code  = $row["API_provider_code"];
				$data[$i]->API_provider_tool_code = $row["API_provider_tool_code"];//código del servicio en la API.
				
				$data[$i]->ser_type_code = $row["ser_type_code"];
				$data[$i]->ser_type_name = $row["ser_type_name"];
				$data[$i]->ser_type_name_e = $row["ser_type_name_e"];
				$data[$i]->ser_code = $row["ser_code"];
				$data[$i]->ser_name = $row["ser_name"];
				$data[$i]->ser_name_e = $row["ser_name_e"];
				$data[$i]->ser_price = $row["ser_price"];
				$data[$i]->ser_price_us = $row["ser_price_us"];
				$data[$i]->ser_price_bol = $row["ser_price_bol"];
								
				$i++;
			}			
			return $data;
		}
		
		/**
		*	Función responsable de obtener el codigo de la API y el codigo del servicio en la API si es que el servicio en M-U está relacionado con una API.
		*	@parameter:	$ser_code	--> codigo del servicio en M-U.
		*	@autor: jsl.	
		*	@date: 16 AGO 2011.
		*/
		public function isToolAPI($ser_code){
			$sql = "SELECT at.API_provider_code, at.API_provider_tool_code FROM api_tool at WHERE at.MU_tool_code=".$ser_code.";";
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result);			
			$data->API_provider_code = $row["API_provider_code"];
			$data->API_provider_tool_code = $row["API_provider_tool_code"];
			return $data;
		}
		
		/**
		 *	Esta función se encarga de insertar el codigo con el cual queda la liberacion de una API en Master-Unlock.
		 *	@parameter: $data->API_provider_code	--> Código de la API.
		 *				$data->API_order_id			--> ID de la liberación en la API.
		 *				$data->lib_code				--> ID de la liberación en Master-Unlock. 
		 *	@autor: jsl.
		 *	@date: 16 AGO 2011
		 */
		public function insertIdOrderAPI($data){
			$sql = "
			UPDATE operator_notice SET API_provider_code = '".$data->API_provider_code."', API_order_id = '".$data->API_order_id."' 
			WHERE libimei_code=".$data->libimei_code.";";
			return mysql_query($sql);
		}
		
		/**
		*	Función responsable de obtener el codigo de un mobile de unlockBase a partir de un codigo de mobile de Master-Unlock.
		*	@parameter:	$MU_mobile_code	--> codigo del mobile en M-U.
		*	@autor: jsl.	
		*	@date: 16 AGO 2011.
		*/
		public function getMobileUnlockBase($MU_mobile_code,$API_provider_code){
			$sql = "
			SELECT am.API_provider_mobile_code FROM api_mobile am WHERE am.MU_mobile_code=".$MU_mobile_code." AND am.API_provider_code=".$API_provider_code.";";
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result);						
			return $row["API_provider_mobile_code"];
		}
		
		/**
		 *	Esta función se encarga de obtener los datos de  un servicio en Master-unlock. obtiene el codigo de la API, del grupo y del servicio en unlockbase
		 *	si es que está relacionado con este.
		 *	@parameter: $ser_code	-->	código del servicio.
		 *	@autor: JSLP.
		 *	@date: 22 AGO 2011.
		 */
		public function getServiceMU($ser_code){
			$sql = "
			SELECT t1.API_tool_code, t1.API_provider_code, t1.API_provider_tool_code, t1.API_provider_tool_name, t1.ser_type_code, t1.ser_type_name,
			t1.ser_type_name_e, t1.ser_code, t1.ser_name, t1.ser_name_e, t1.ser_price, t1.ser_price_us, t1.ser_price_bol, t1.ser_algoritmo, t1.ser_form_liberation, 
			IFNULL(ag.API_provider_group_code,0) AS API_provider_group_code, ag.API_provider_group_name
			FROM
			(SELECT at.API_tool_code, IFNULL(at.API_provider_code,0) AS API_provider_code, IFNULL(at.API_provider_tool_code,0) AS API_provider_tool_code,
			at.API_provider_tool_name, t0.ser_type_code, t0.ser_type_name,	t0.ser_type_name_e,	t0.ser_code, t0.ser_name, t0.ser_name_e, t0.ser_price, 
			t0.ser_price_us, t0.ser_price_bol, t0.ser_algoritmo, t0.ser_form_liberation 
			FROM
			(SELECT st.ser_type_code, st.ser_type_name, st.ser_type_name_e, s.ser_code, s.ser_name, s.ser_name_e, s.ser_price, s.ser_price_us, s.ser_price_bol, s.ser_algoritmo, s.ser_form_liberation
			FROM service_type st JOIN services s ON st.ser_type_code=s.ser_type_code WHERE s.ser_code=".$ser_code.") t0
			LEFT JOIN api_tool at 
			ON at.MU_tool_code=t0.ser_code ORDER BY t0.ser_type_code, t0.ser_type_name, t0.ser_name, t0.ser_price) t1
			LEFT JOIN api_group ag ON t1.ser_type_code = ag.MU_group_code;";
			//die($sql);
			$result = mysql_query($sql);			
			$row=mysql_fetch_array($result);
			
			$data->API_provider_code  = $row["API_provider_code"];
			$data->API_tool_code = $row["API_tool_code"];
			
			$data->API_provider_group_code = $row["API_provider_group_code"];//cod del grupo en la API.
			$data->API_provider_group_name = $row["API_provider_group_name"];
						
			$data->API_provider_tool_code = $row["API_provider_tool_code"];//código del servicio en la API.
			$data->API_provider_tool_name = $row["API_provider_tool_name"];			
			
			$data->ser_type_code = $row["ser_type_code"];
			$data->ser_type_name = $row["ser_type_name"];
			$data->ser_type_name_e = $row["ser_type_name_e"];
			$data->ser_code = $row["ser_code"];
			$data->ser_name = $row["ser_name"];
			$data->ser_name_e = $row["ser_name_e"];
			$data->ser_price = $row["ser_price"];
			$data->ser_price_us = $row["ser_price_us"];
			$data->ser_price_bol = $row["ser_price_bol"];
			
			$data->ser_algoritmo = $row["ser_algoritmo"];
			$data->ser_form_liberation = $row["ser_form_liberation"];
			
			return $data;
		}
		
		/**
		 *	Esta función es la encargada de obtener la informacion de un mobile en Master-Unlock, para la API obtiene cod y nombre si el mobile tiene relacion alguna.
		 *	@parameter: $mod_code	--> es el cod del servicio en master-unlock.
		 *	@autor: JSLP.
		 *	@date: 22 AGO 2011.
		 */
		public function getMobileMU($mod_code,$API_provider_code){
			$sql = "
			SELECT m.mod_code, m.bra_code, m.mod_name, t.API_provider_mobile_code, t.API_provider_mobile_name FROM model m 
			LEFT JOIN 
			(SELECT am.API_provider_mobile_code, am.API_provider_mobile_name, am.MU_mobile_code FROM api_mobile am WHERE am.API_provider_code=".$API_provider_code.") t
			ON m.mod_code=t.MU_mobile_code WHERE m.mod_code=".$mod_code.";";
			
			$result = mysql_query($sql);			
			$row=mysql_fetch_array($result);
			
			$data->mod_code = $row["mod_code"];
			$data->bra_code = $row["bra_code"];
			$data->mod_name = $row["mod_name"];
			$data->API_provider_mobile_code = $row["API_provider_mobile_code"];
			$data->API_provider_mobile_name = $row["API_provider_mobile_name"];
			
			return $data;
		}
		
		/**
		 *	Esta función es la encargada de obtener la informacion de un network en Master-Unlock, para la API obtiene cod y nombre si el network tiene 
		 *	relacion con esta.
		 *	@parameter: $mod_code	--> es el cod del servicio en master-unlock.
		 *	@autor: JSLP.
		 *	@date: 22 AGO 2011.
		 */
		public function getNetworkMU($red_id,$API_provider_code){
			$sql = "
			SELECT r.red_id, r.red_name, an.API_provider_network_code, an.API_provider_network_name FROM red r
			LEFT JOIN api_network an ON r.red_id=an.MU_network_code WHERE r.red_id=".$red_id." AND an.API_provider_code=".$API_provider_code.";";
			$result = mysql_query($sql);			
			$row=mysql_fetch_array($result);
			
			$data->red_id = $row["red_id"];
			$data->red_name = $row["red_name"];
			$data->API_provider_network_code = $row["API_provider_network_code"];
			$data->API_provider_network_name = $row["API_provider_network_name"];
			
			return $data;
		}
		
		/**
		 *	Esta función es la encargada de obtener los paises en master-unlock correspondientes a unos operadores de una api que llegan como parámetro.
		 *	@parameter: $API_provider_code	-->código de la API.
		 *				$array_network	--> cadena contenedora de los códigos de los network que se necesita saber el país.
		 *	@autor: JSLP.
		 *	@date: 23 AGO 2011.
		 *	@return: array con los paises correspondientes a los network de unlockbase que llegan como parámetro.
		 */
		public function getCountryMUByNetworkAPI($array_network,$API_provider_code){
			$sql = "
			SELECT c.cou_code, c.cou_name FROM country c JOIN
			
			(SELECT ac.API_provider_country_code, ac.MU_country_code FROM api_country ac JOIN
			(SELECT an.API_provider_country_code FROM api_network an 
			WHERE an.API_provider_code=".$API_provider_code." AND an.API_provider_network_code IN ".$array_network." GROUP BY an.API_provider_country_code) t
			ON t.API_provider_country_code=ac.API_provider_country_code WHERE ac.MU_country_code > 0 AND ac.API_provider_code=".$API_provider_code.") t1
			
			ON c.cou_code=t1.MU_country_code ORDER BY c.cou_name;";
			//die($sql);
			$result = mysql_query($sql);			
			$data;
			$i=0;
			while($row=mysql_fetch_array($result)){
				$data[$i]->code = $row['cou_code'];
				$data[$i]->name = $row['cou_name'];
				$i++;
			}
			return $data;
		}
		
		/**
		 *	Esta función es la encargada de obtener los network de master-unlock correspondientes a unos operadores de unlockbase que llegan como parámetro.
		 *	@parameter: $API_provider_code	-->código de la API.
		 *				$array_network	--> cadena contenedora de los códigos de los network de unlockbase que se necesita saber el correspondiente en master-unlock.
		 *				$cou_code	--> codigo del pais para filtrar los networks.
		 *	@autor: JSLP.
		 *	@date: 23 AGO 2011.
		 *	@return: array con los paises correspondientes a los network que llegan como parámetro.
		 */
		public function getNetworkMUByNetworkAPI($array_network,$cou_code,$API_provider_code){
			$sql = "
			SELECT r.red_id, r.red_name FROM red r
			JOIN
			(SELECT an.API_provider_network_code, an.MU_network_code FROM api_network an
			WHERE an.API_provider_code=".$API_provider_code." AND an.API_provider_network_code IN ".$array_network." AND an.MU_network_code > 0) t
			ON r.red_id=t.MU_network_code WHERE r.cou_code=".$cou_code.";";
			
			$result = mysql_query($sql);			
			$data;
			$i=0;
			while($row=mysql_fetch_array($result)){
				$data[$i]->code = $row['red_id'];
				$data[$i]->name = $row['red_name'];
				$i++;
			}
			return $data;
		}
		
		/**
		 *	Esta función es la encargada de obtener el listado de liberaciones que son por API y que están pendientes por liberar.
		 *	@autor: JSLP.
		 *	@date: 23 AGO 2011.
		 *	@return: lista con las liberaciones pendientes y que son por API.
		*/ 
		public function getListOrderWaitingMU_API($init=0,$amount=0,$search='',$order_by=''){
			
			include_once("RijndaelHex.php");
			$rijndael = new RijndaelHex();
		
			$where = "";
			if($search != '')
				$where = " WHERE (api_name like '%".$search."%' OR
								  cou_name like '%".$search."%' OR
								  red_name like '%".$search."%' OR
								  bra_name like '%".$search."%' OR
								  mod_name like '%".$search."%' OR
								  cli_email like '%".$search."%' OR
								  ser_name like '%".$search."%' OR
								  API_order_id like '%".$search."%' OR
								  libimei_number like '%".$search."%') ";
			
			$sql = "
			SELECT t8.libimei_code, t8.lib_code, t8.libimei_date, t8.libimei_number, t8.libimei_number_liberation, t8.libimei_status, t8.libimei_bonus,
			t8.libimei_verification, t8.API_provider_code, t8.API_order_id, t8.libimeir_code_cliente_reseller, t8.libimeir_code_imei, t8.reseller_code, t8.ser_code ,
			t8.cli_code, t8.lib_country, t8.lib_model, t8.lib_operator, t8.lib_numMep, t8.lib_description, t8.ser_name, t8.mod_code, t8.mod_name, t8.bra_code, t8.bra_name,
			t8.red_id, t8.red_name, t8.cou_code, t8.cou_name, t8.API_name, c.cli_email, c.cli_language_email
			FROM
			(SELECT t7.libimei_code, t7.lib_code, t7.libimei_date, t7.libimei_number, t7.libimei_number_liberation, t7.libimei_status, t7.libimei_bonus,
			t7.libimei_verification, t7.API_provider_code, t7.API_order_id, t7.libimeir_code_cliente_reseller, t7.libimeir_code_imei, t7.reseller_code, t7.ser_code, 
			t7.cli_code, t7.lib_country, t7.lib_model, t7.lib_operator, t7.lib_numMep, t7.lib_description, t7.ser_name, t7.mod_code, t7.mod_name, t7.bra_code, t7.bra_name,
			t7.red_id, t7.red_name, t7.cou_code, t7.cou_name, a.API_name
			FROM
			(SELECT t6.libimei_code, t6.lib_code, t6.libimei_date, t6.libimei_number, t6.libimei_number_liberation, t6.libimei_status, t6.libimei_bonus,
			 t6.libimei_verification, t6.API_provider_code, t6.API_order_id, t6.libimeir_code_cliente_reseller, t6.libimeir_code_imei, t6.reseller_code, t6.ser_code ,
			 t6.cli_code, t6.lib_country, t6.lib_model, t6.lib_operator, t6.lib_numMep, t6.lib_description, t6.ser_name, t6.mod_code, t6.mod_name, t6.bra_code, t6.bra_name,
			 t6.red_id, t6.red_name, t6.cou_code, c.cou_name
			FROM
			(SELECT t5.libimei_code, t5.lib_code, t5.libimei_date, t5.libimei_number, t5.libimei_number_liberation, t5.libimei_status, t5.libimei_bonus,
			 t5.libimei_verification, t5.API_provider_code, t5.API_order_id, t5.libimeir_code_cliente_reseller, t5.libimeir_code_imei, t5.reseller_code,t5.ser_code ,
			 t5.cli_code, t5.lib_country, t5.lib_model, t5.lib_operator, t5.lib_numMep, t5.lib_description, t5.ser_name,t5.mod_code, t5.mod_name, t5.bra_code, t5.bra_name,
			 r.red_id, r.red_name, r.cou_code
			FROM
			(SELECT t4.libimei_code, t4.lib_code, t4.libimei_date, t4.libimei_number, t4.libimei_number_liberation, t4.libimei_status, t4.libimei_bonus,
			 t4.libimei_verification, t4.API_provider_code, t4.API_order_id, t4.libimeir_code_cliente_reseller, t4.libimeir_code_imei, t4.reseller_code, t4.ser_code,
			 t4.cli_code, t4.lib_country, t4.lib_model, t4.lib_operator, t4.lib_numMep, t4.lib_description, t4.ser_name, t4.mod_code, t4.mod_name, t4.bra_code, b.bra_name
			FROM
			(SELECT t3.libimei_code, t3.lib_code, t3.libimei_date,t3.libimei_number, t3.libimei_number_liberation, t3.libimei_status, t3.libimei_bonus, 
			 t3.libimei_verification, t3.API_provider_code, t3.API_order_id, t3.libimeir_code_cliente_reseller, t3.libimeir_code_imei, t3.reseller_code, t3.ser_code,
			 t3.cli_code, t3.lib_country, t3.lib_model, t3.lib_operator, t3.lib_numMep, t3.lib_description, t3.ser_name, m.mod_code, m.mod_name, m.bra_code
			FROM
			(SELECT t1.libimei_code, t1.lib_code, t1.libimei_date, t1.libimei_number, t1.libimei_number_liberation, t1.libimei_status, t1.libimei_bonus,
			 t1.libimei_verification, t1.API_provider_code, t1.API_order_id, t1.libimeir_code_cliente_reseller, t1.libimeir_code_imei, t1.reseller_code,t1.ser_code ,
			 t1.cli_code, t1.lib_country, t1.lib_model, t1.lib_operator, t1.lib_numMep, t1.lib_description, s.ser_name
			FROM
			(SELECT t.libimei_code, t.lib_code, t.libimei_date,t.libimei_number, t.libimei_number_liberation, t.libimei_status, t.libimei_bonus, 
			 t.libimei_verification, t.API_provider_code, t.API_order_id, t.libimeir_code_cliente_reseller, t.libimeir_code_imei, t.reseller_code, l.ser_code , l.cli_code,
			 l.lib_country, l.lib_model, l.lib_operator, l.lib_numMep, l.lib_description
			FROM
			(SELECT libimei.libimei_code, libimei.lib_code, libimei.libimei_date, libimei.libimei_number, libimei.libimei_number_liberation, libimei.libimei_status,
			libimei.libimei_bonus, libimei.libimei_verification, libimei.API_provider_code, libimei.API_order_id, lr.libimeir_code_cliente_reseller, lr.libimeir_code_imei,
			lr.reseller_code
			FROM operator_notice libimei LEFT JOIN liberation_imei_reseller lr ON libimei.libimei_code=lr.libimei_code
			WHERE libimei.API_provider_code IS NOT NULL AND (libimei.libimei_status=0 OR libimei.libimei_status=2) AND libimei.API_order_id IS NOT NULL) t
			JOIN liberations l ON t.lib_code=l.lib_code) t1			
			JOIN services s ON t1.ser_code=s.ser_code) t3
			LEFT JOIN model m ON t3.lib_model=m.mod_code) t4
			LEFT JOIN brand b ON t4.bra_code=b.bra_code) t5
			LEFT JOIN red r ON t5.lib_country=r.red_id) t6
			LEFT JOIN country c ON t6.cou_code=c.cou_code) t7
			JOIN api a ON t7.API_provider_code=a.api_code) t8
			JOIN client c ON t8.cli_code=c.cli_code ".$where.$order_by." ";
			//die($sql);
			if(!($init==0 && $amount==0))
				$sql .= " LIMIT ".$init.",".$amount.";";
			
			if($result = mysql_query($sql)){			
				$i=0;
				while($row = mysql_fetch_array($result)){
					$data[$i]->libimei_code = $row["libimei_code"];
					$data[$i]->lib_code = $row["lib_code"];
					$data[$i]->libimei_date = $row["libimei_date"];
					$data[$i]->libimei_number = $rijndael->lindecrypthex($row["libimei_number"]);
					$data[$i]->libimei_number_liberation = $row["libimei_number_liberation"];
					$data[$i]->libimei_status = $row["libimei_status"];
					$data[$i]->libimei_bonus = $row["libimei_bonus"];
					$data[$i]->libimei_verification = $row["libimei_verification"];
					$data[$i]->API_provider_code = $row["API_provider_code"];
					$data[$i]->API_order_id = $row["API_order_id"];//ID de la orden en la API.
					
					$data[$i]->libimeir_code_cliente_reseller = $row["libimeir_code_cliente_reseller"];//codigo del cliente del reseller, puede ser null
					$data[$i]->libimeir_code_imei = $row["libimeir_code_imei"];//codigo de la liberacion en la BD del reseller
					$data[$i]->reseller_code = $row["reseller_code"];//codigo del reseller
					
					$data[$i]->ser_code = $row["ser_code"];
					$data[$i]->cli_code = $row["cli_code"];
					$data[$i]->cli_email = $rijndael->lindecrypthex($row["cli_email"]);
					$data[$i]->cli_language_email = $row["cli_language_email"];
					$data[$i]->lib_country = $row["lib_country"];
					$data[$i]->lib_model = $row["lib_model"];
					$data[$i]->lib_operator = $row["lib_operator"];
					$data[$i]->lib_numMep = $row["lib_numMep"];
					$data[$i]->lib_description = $row["lib_description"];
					
					$data[$i]->ser_name = $row["ser_name"];
					$data[$i]->cli_email = $row["cli_email"];
					$data[$i]->mod_code = $row["mod_code"];
					$data[$i]->mod_name = $row["mod_name"];
					$data[$i]->bra_code = $row["bra_code"];
					$data[$i]->bra_name = $row["bra_name"];
					$data[$i]->red_id = $row["red_id"];
					$data[$i]->red_name = $row["red_name"];
					$data[$i]->cou_code = $row["cou_code"];
					$data[$i]->cou_name = $row["cou_name"];
					
					$data[$i]->api_name = $row["API_name"];
					
					$i++;
				}
				return $data;
			}
			else
				return NULL;
		}
		
		/**
		 *	Esta función es la encargada de obtener los datos de una liberación por API.
		 *	@parameter: $libimei_code	--> ID de la liberacion en master unlock
		 *	@autor: JSLP.
		 *	@date: 23 AGO 2011.
		 *	@return: lista con las liberaciones pendientes y que son por API.
		*/ 
		function getOrderMU_API($libimei_code){
			
			include_once("RijndaelHex.php");
			$rijndael = new RijndaelHex();
			
			$sql = "
			SELECT t8.libimei_code, t8.lib_code, t8.libimei_date, t8.libimei_number, t8.libimei_number_liberation, t8.libimei_status, t8.libimei_bonus,
			t8.libimei_verification, t8.API_provider_code, t8.API_order_id, t8.ser_code , t8.cli_code, t8.lib_country, t8.lib_model, t8.lib_operator, 
			t8.lib_numMep, t8.lib_description, t8.ser_name, t8.cli_email, t8.mod_code, t8.mod_name, t8.bra_code, t8.bra_name, t8.red_id, t8.red_name, t8.cou_code,
			t8.cou_name, t8.API_name, c.cli_email, c.cli_language_email
			FROM
			(SELECT t7.libimei_code, t7.lib_code, t7.libimei_date, t7.libimei_number, t7.libimei_number_liberation, t7.libimei_status, t7.libimei_bonus,
			t7.libimei_verification, t7.API_provider_code, t7.API_order_id, t7.ser_code , t7.cli_code, t7.lib_country, t7.lib_model, t7.lib_operator, 
			t7.lib_numMep, t7.lib_description, t7.ser_name, t7.cli_email, t7.mod_code, t7.mod_name, t7.bra_code, t7.bra_name, t7.red_id, t7.red_name, t7.cou_code,
			t7.cou_name, a.API_name
			FROM
			(SELECT t6.libimei_code, t6.lib_code, t6.libimei_date, t6.libimei_number, t6.libimei_number_liberation, t6.libimei_status, t6.libimei_bonus,
			 t6.libimei_verification, t6.API_provider_code, t6.API_order_id, t6.ser_code , t6.cli_code, t6.lib_country, t6.lib_model, t6.lib_operator,
			 t6.lib_numMep, t6.lib_description, t6.ser_name, t6.cli_email, t6.mod_code, t6.mod_name, t6.bra_code, t6.bra_name, t6.red_id, t6.red_name, 
			 t6.cou_code, c.cou_name
			FROM
			(SELECT t5.libimei_code, t5.lib_code, t5.libimei_date, t5.libimei_number, t5.libimei_number_liberation, t5.libimei_status, t5.libimei_bonus,
			 t5.libimei_verification, t5.API_provider_code, t5.API_order_id, t5.ser_code , t5.cli_code, t5.lib_country, t5.lib_model, t5.lib_operator, 
			 t5.lib_numMep, t5.lib_description, t5.ser_name, t5.cli_email,t5.mod_code, t5.mod_name, t5.bra_code, t5.bra_name, r.red_id, r.red_name, r.cou_code
			FROM
			(SELECT t4.libimei_code, t4.lib_code, t4.libimei_date, t4.libimei_number, t4.libimei_number_liberation, t4.libimei_status, t4.libimei_bonus,
			 t4.libimei_verification, t4.API_provider_code, t4.API_order_id, t4.ser_code , t4.cli_code, t4.lib_country, t4.lib_model, t4.lib_operator, 
			 t4.lib_numMep, t4.lib_description, t4.ser_name, t4.cli_email, t4.mod_code, t4.mod_name, t4.bra_code, b.bra_name
			FROM
			(SELECT t3.libimei_code, t3.lib_code, t3.libimei_date,t3.libimei_number, t3.libimei_number_liberation, t3.libimei_status, t3.libimei_bonus, 
			 t3.libimei_verification, t3.API_provider_code, t3.API_order_id, t3.ser_code , t3.cli_code, t3.lib_country, t3.lib_model, t3.lib_operator, 
			 t3.lib_numMep, t3.lib_description, t3.ser_name, t3.cli_email, m.mod_code, m.mod_name, m.bra_code
			FROM
			(SELECT t2.libimei_code, t2.lib_code, t2.libimei_date, t2.libimei_number, t2.libimei_number_liberation, t2.libimei_status, t2.libimei_bonus,
			 t2.libimei_verification, t2.API_provider_code, t2.API_order_id, t2.ser_code , t2.cli_code, t2.lib_country, t2.lib_model, t2.lib_operator,
			 t2.lib_numMep, t2.lib_description, t2.ser_name, c.cli_email
			FROM
			(SELECT t1.libimei_code, t1.lib_code, t1.libimei_date, t1.libimei_number, t1.libimei_number_liberation, t1.libimei_status, t1.libimei_bonus,
			 t1.libimei_verification, t1.API_provider_code, t1.API_order_id, t1.ser_code , t1.cli_code, t1.lib_country, t1.lib_model, t1.lib_operator,
			 t1.lib_numMep, t1.lib_description, s.ser_name
			FROM
			(SELECT t.libimei_code, t.lib_code, t.libimei_date,t.libimei_number, t.libimei_number_liberation, t.libimei_status, t.libimei_bonus, 
			 t.libimei_verification, t.API_provider_code, t.API_order_id, l.ser_code , l.cli_code, l.lib_country, l.lib_model, l.lib_operator, l.lib_numMep, 
			 l.lib_description
			FROM			
			(SELECT libimei.libimei_code, libimei.lib_code, libimei.libimei_date, libimei.libimei_number, libimei.libimei_number_liberation, libimei.libimei_status,
			 libimei.libimei_bonus, libimei.libimei_verification, libimei.API_provider_code, libimei.API_order_id FROM operator_notice libimei 
			WHERE libimei.libimei_code=".$libimei_code.") t
			JOIN liberations l ON t.lib_code=l.lib_code) t1
			JOIN services s ON t1.ser_code=s.ser_code) t2
			JOIN client c ON t2.cli_code=c.cli_code) t3
			LEFT JOIN model m ON t3.lib_model=m.mod_code) t4
			LEFT JOIN brand b ON t4.bra_code=b.bra_code) t5
			LEFT JOIN red r ON t5.lib_country=r.red_id) t6
			LEFT JOIN country c ON t6.cou_code=c.cou_code) t7
			JOIN api a ON t7.API_provider_code=a.api_code) t8
			JOIN client c ON t8.cli_code=c.cli_code;";
			
			if($result = mysql_query($sql)){			
				$row = mysql_fetch_array($result);
	
				$data->libimei_code = $row["libimei_code"];
				$data->lib_code = $row["lib_code"];
				$data->libimei_date = $row["libimei_date"];
				$data->libimei_number = $rijndael->lindecrypthex($row["libimei_number"]);
				$data->libimei_number_liberation = $row["libimei_number_liberation"];//codigo de liberacion
				$data->libimei_status = $row["libimei_status"];
				$data->libimei_bonus = $row["libimei_bonus"];
				$data->libimei_verification = $row["libimei_verification"];
				$data->API_provider_code = $row["API_provider_code"];
				$data->API_order_id = $row["API_order_id"];//ID de la orden en la API.
				
				$data->ser_code = $row["ser_code"];
				$data->cli_code = $row["cli_code"];
				$data->cli_email = $row["cli_email"];			
				$data->cli_language_email = $row["cli_language_email"];
				
				$data->lib_country = $row["lib_country"];
				$data->lib_model = $row["lib_model"];
				$data->lib_operator = $row["lib_operator"];
				$data->lib_numMep = $row["lib_numMep"];
				$data->lib_description = $row["lib_description"];
				
				$data->ser_name = $row["ser_name"];
				$data->cli_email = $row["cli_email"];
				$data->mod_code = $row["mod_code"];
				$data->mod_name = $row["mod_name"];
				$data->bra_code = $row["bra_code"];
				$data->bra_name = $row["bra_name"];
				$data->red_id = $row["red_id"];
				$data->red_name = $row["red_name"];
				$data->cou_code = $row["cou_code"];
				$data->cou_name = $row["cou_name"];
				
				$data->api_name = $row["API_name"];
				return $data;
			}
			else{
				return NULL;				
			}
		}
		
		/**
		*	Función encargada de verificar por nombre si una marca existe o no, si esta existe retorna el id de la marca.
		*	@autor: JSL
		*	@date: 16/01/2012
		*/
		function issetBrandMU($bra_name){
			$sql = "SELECT b.bra_code FROM brand b WHERE b.bra_name='".$bra_name."'";
			if ( $result = mysql_query ($sql) ){
				$row = mysql_fetch_array ( $result );
				return (int) $row["bra_code"];
			}
			else
				return false;
		}
		
		/**
		*	Esta función inserta una marca en Master-Unlock. retorna true o false, dependiendo de si tubo o no exito al insertar.
		*	@autor: jsl
		*	@date: 17/01/2012
		*/
		function insertBrandMU($bra_name){
			$sql = "INSERT INTO brand (bra_code, bra_name) VALUES (NULL, '".$bra_name."')";
			if ( $result = mysql_query( $sql ) )
				return mysql_insert_id();
			else
				return false;
		}
		
		/**
		*	Esta función inserta una modelo en Master-Unlock. retorna true o false, dependiendo de si tubo o no exito al insertar.
		*	@autor: jsl
		*	@date: 17/01/2012
		*/
		function insertModelMU($data){
			$sql = "INSERT INTO model (mod_code, bra_code, mod_name) VALUES (NULL, ".$data->bra_code.",'".$data->mod_name."')";
			if ( $result = mysql_query( $sql ) )
				return mysql_insert_id();
			else
				return false;
		}
		
		/**
		*	Función encargada de verificar si una marca de existe en api_brand (marcas que proveen las API's), devuelve el id de la marca de la API si esta existe.
		*	@autor: JSL
		*	@date: 17/01/2012
		*/
		function issetBrandAPI($data){
			$sql = "SELECT ab.API_brand_code FROM api_brand ab WHERE ab.API_provider_code = ".$data->API_provider_code." 
						AND ab.API_provider_brand_code='".$data->API_provider_brand_code."'";
			
			if ( $result = mysql_query ($sql) ){
				$row = mysql_fetch_array ( $result );
				return (int) $row["API_brand_code"];
			}
			else
				return false;
		}
		
		/**
		*	Esta funcion inserta un nuevo registro en api_brand.
		*	@autor: jsl.
		*	@date: 17/07/2011
		*/
		function insertApiBrand($data){
			$sql = "INSERT INTO api_brand (API_brand_code, API_provider_code, MU_brand_code, API_provider_brand_code, API_provider_brand_name) VALUES
				(NULL, ".$data->API_provider_code.", ".$data->MU_brand_code.", ".$data->API_provider_brand_code.", '".$data->API_provider_brand_name."');";
			
			if ( $result = mysql_query( $sql ) )
				return mysql_insert_id();
			else
				return false;
		}
		
		/**
		*	Esta Función verifica que si una marca de UB esta relacionada con una en MU, en caso de ser cierto retorna el id de la marca en MU.
		*	@autor: jsl.
		*	@date: 17/11/2012
		*/
		function isBrandSync( $API_brand_code ){
			$sql = "SELECT ab.MU_brand_code FROM api_brand ab WHERE ab.API_brand_code=".$API_brand_code;
			if ( $result = mysql_query ($sql) ){
				$row = mysql_fetch_array ( $result );
				return (int) $row["MU_brand_code"];
			}
			else
				return false;
		}
		
		/**
		*	Esta función retorna el id de un modelo en caso de que coincida con nombre y marca que llegan como parametro. si no lo encuentra retorna 0.
		*	@autor: JSL
		*	@date: 17/01/2012
		*/
		function issetModelMU($data){
			$sql = "SELECT m.mod_code FROM model m WHERE m.bra_code=".$data->bra_code." AND m.mod_name='".$data->mod_name."'";
			if ( $result = mysql_query ($sql) ){
				$row = mysql_fetch_array ( $result );
				return (int) $row["mod_code"];
			}
			else
				return false;
		}
		
		/**
		*	Función encargada de verificar si un modelo existe en api_mobile (modelos que proveen las API's), devuelve el id del modelo de la API si este existe.
		*	@autor: JSL
		*	@date: 17/01/2012
		*/
		function issetModelAPI($data){
			$sql = "SELECT am.API_mobile_code FROM api_mobile am WHERE am.API_provider_code = ".$data->API_provider_code." 
						AND am.API_provider_mobile_code='".$data->API_provider_mobile_code."'";
			
			if ( $result = mysql_query ($sql) ){
				$row = mysql_fetch_array ( $result );
				return (int) $row["API_mobile_code"];
			}
			else
				return false;
		}
		
		/**
		*	Esta función es la encargada de insertar un nuevo registro en api_mobile.
		*	@autor: JSL
		*	@date: 17/01/2012
		*/
		function insertModelAPI($data){
			$sql = "INSERT INTO api_mobile (API_mobile_code, API_provider_code, MU_mobile_code, API_provider_brand_code, API_provider_mobile_code, API_provider_mobile_name)
				VALUES (NULL, ".$data->API_provider_code.", ".$data->MU_mobile_code.", ".$data->API_provider_brand_code.", ".$data->API_provider_mobile_code.", 
				'".$data->API_provider_mobile_name."')";
			if ( mysql_query ($sql) ){
				return mysql_insert_id();
			}
			else
				return false;
		}
		
		/**
		*	Esta Función verifica que si un modelo de UB esta relacionada con uno en MU, en caso de ser cierto retorna el id de del modelo en MU.
		*	@autor: jsl.
		*	@date: 18/11/2012
		*/
		function isMobileSync( $API_mobile_code ){
			$sql = "SELECT am.MU_mobile_code FROM api_mobile am WHERE am.API_mobile_code=".$API_mobile_code;
			if ( $result = mysql_query ($sql) ){
				$row = mysql_fetch_array ( $result );
				return (int) $row["MU_mobile_code"];
			}
			else
				return false;
		}
		
		/**
		*	Función resposable de validar por nombre si un servicio existe o no en una API especifica.
		*	@autor: JSL
		*	@date: 21/02/2012
		*/
		function issetAPI_tool($data){
			$sql = "SELECT at.API_tool_code FROM api_tool at WHERE at.API_provider_code=".$data->API_provider_code." AND 
						at.API_provider_tool_code='".$data->API_provider_tool_code."';";
					
			if ( $result = mysql_query ($sql) ){
				$row = mysql_fetch_array ( $result );
				return (int) $row["API_tool_code"];
			}
			else
				return false;
		}
		
		/**
		*	Función responsable de insertar un nuevo registro en api_tool
		*	@autor: JSL
		*	@date: 21/02/2011
		*/
		function insertApi_tool($data){
			$sql = "INSERT INTO api_tool (API_tool_code, API_provider_code, MU_tool_code, API_provider_group_code, API_provider_tool_code, API_provider_tool_name) VALUES
			(NULL, ".$data->API_provider_code.", 0, 0, ".$data->API_provider_tool_code.", '".$data->API_provider_tool_name."');";
			
			if ( mysql_query ($sql) )
				return mysql_insert_id();
			else
				return false;
		}
		
		/**
		*	Función responsable de validar por nombre si un pais existe en la aplicacion
		*	@autor: JSL
		*	@date: 21/02/2012
		*/
		function issetCountryMU($cou_name){
			$sql = "SELECT c.cou_code FROM country c WHERE c.cou_name='".$cou_name."';";
					
			if ( $result = mysql_query ($sql) ){
				$row = mysql_fetch_array ( $result );
				return (int) $row["cou_code"];
			}
			else
				return false;
		}
		
		/**
		*	Función responsable insertar un nuevo pais en la aplicacion
		*	@autor: JSL
		*	@date: 21/02/2012
		*/
		function insertCountryMU($cou_name){
			$sql = "INSERT INTO country ( cou_code, cou_name) VALUES ( NULL, '".$cou_name."' );";
					
			if ( mysql_query ($sql) ){
				return mysql_insert_id ();
			}
			else
				return false;
		}
		
		/**
		*	Función resposable de validar por nombre si un operador o red existe o no en master-unlock.
		*	@autor: JSL
		*	@date: 21/02/2012
		*/
		function issetNetworkMU($data){
			$sql = "SELECT r.red_id FROM red r WHERE r.cou_code=".$data->cou_code." AND r.red_name='".$data->red_name."';";
					
			if ( $result = mysql_query ($sql) ){
				$row = mysql_fetch_array ( $result );
				return (int) $row["red_id"];
			}
			else
				return false;
		}
		
		/**
		*	Función responsable insertar un nuevo operador en la aplicacion
		*	@autor: JSL
		*	@date: 22/02/2012
		*/
		function insertNetworkMU($data){
			$sql = "INSERT INTO red ( red_id, cou_code, red_name) VALUES ( NULL, ".$data->cou_code.", '".$data->red_name."' );";
					
			if ( mysql_query ($sql) ){
				return mysql_insert_id ();
			}
			else
				return false;
		}
		
		/**
		*	Función encargada de verificar si un operador existe en API_network (operadores que proveen las API's), devuelve el id del operador de la API si este existe.
		*	@autor: JSL
		*	@date: 22/02/2012
		*/
		function issetNetworkAPI($data){
			$sql = "SELECT an.API_network_code FROM api_network an WHERE an.API_provider_code = ".$data->API_provider_code." 
						AND an.API_provider_network_code='".$data->API_provider_network_code."'";
			
			if ( $result = mysql_query ($sql) ){
				$row = mysql_fetch_array ( $result );
				return (int) $row["API_network_code"];
			}
			else
				return false;
		}
		
		/**
		*	Función responsable de contar cuantas veces está relacionado un operador de master-unlock con uno de un proveedor (API).
		*	@autor: JSL
		*	@date: 22/02/2012
		*/
		function count_API_network($data){
			$sql = "SELECT COUNT(an.API_network_code) AS cant FROM api_network an 
					WHERE an.MU_network_code=".$data->new_MU_network_code." AND an.API_provider_code=".$data->API_provider_code.";";
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result);
			return $row["cant"];
		}
		
		/**
		*	Esta función es la encargada de insertar un nuevo registro en api_network.
		*	@autor: JSL
		*	@date: 22/02/2012
		*/
		function insertNetworkAPI($data){
			$sql = "
			INSERT INTO api_network (API_network_code, API_provider_code, MU_network_code, API_provider_country_code, API_provider_network_code, API_provider_network_name) 
			VALUES 
			(NULL, ".$data->API_provider_code.", ".$data->MU_network_code.", ".$data->API_provider_country_code.", ".$data->API_provider_network_code.", 
				'".$data->API_provider_network_name."')";
			
			if ( mysql_query ($sql) ){
				return mysql_insert_id();
			}
			else
				return false;
		}
		
		/**
		*	Esta Función verifica que si un operador de una api esta relacionada con uno en MU, en caso de ser cierto retorna el id de del operador en MU.
		*	@autor: JSL
		*	@date: 22/02/2012
		*/
		function isNetworkSync( $API_network_code ){
			$sql = "SELECT an.MU_network_code FROM api_network an WHERE an.API_network_code=".$API_network_code;
			if ( $result = mysql_query ($sql) ){
				$row = mysql_fetch_array ( $result );
				return (int) $row["MU_network_code"];
			}
			else
				return false;
		}
		
		/**
		*	Función responsable de asociar el codigo de un ooperador en master-unlock con el de un operador en una API.
		*	@autor: JSL
		*	@date: 22/02/2012
		*/
		function update_API_network($data){
			$sql = "UPDATE api_network SET MU_network_code = '".$data->new_MU_network_code."' WHERE API_network_code=".$data->API_network_code.";";
			return mysql_query($sql);
		}
		
		/**
		*	Función encargada de verificar si un pais existe en API_country (marcas que proveen las API's), devuelve el id de la marca de la API si esta existe.
		*	@autor: JSL
		*	@date: 22/02/2012
		*/
		function issetCountryAPI($data){
			$sql = "SELECT ac.API_country_code FROM api_country ac WHERE ac.API_provider_code = ".$data->API_provider_code." 
						AND ac.API_provider_country_code='".$data->API_provider_country_code."'";
			
			if ( $result = mysql_query ($sql) ){
				$row = mysql_fetch_array ( $result );
				return (int) $row["API_country_code"];
			}
			else
				return false;
		}
		
		/**
		*	Esta funcion inserta un nuevo registro en api_country.
		*	@autor: JSL
		*	@date: 22/02/2012
		*/
		function insertApiCountry($data){
			$sql = "INSERT INTO api_country (API_country_code, API_provider_code, MU_country_code, API_provider_country_code, API_provider_country_name) VALUES
				(NULL, ".$data->API_provider_code.", ".$data->MU_country_code.", ".$data->API_provider_country_code.", '".$data->API_provider_country_name."');";
			
			if ( $result = mysql_query( $sql ) )
				return mysql_insert_id();
			else
				return false;
		}
		
		/**
		*	Esta Función verifica que si un pais de la api esta relacionada con uno en MU, en caso de ser cierto retorna el id del pais en MU.
		*	@autor: JSL
		*	@date: 22/02/2012
		*/
		function isCountrySync( $API_country_code ){
			$sql = "SELECT ac.MU_country_code FROM api_country ac WHERE ac.API_country_code=".$API_country_code;
			if ( $result = mysql_query ($sql) ){
				$row = mysql_fetch_array ( $result );
				return (int) $row["MU_country_code"];
			}
			else
				return false;
		}
		
		/**
		*	Función responsable de contar cuantas veces está relacionado un pais de master-unlock con uno de un proveedor (API).
		*	@autor: JSL
		*	@date: 22/02/2012
		*/
		function count_API_country($data){
			$sql = " 
			SELECT COUNT(ac.API_country_code) AS cant FROM api_country ac WHERE ac.MU_country_code=".$data->new_MU_country_code." 
				AND ac.API_provider_code=".$data->API_provider_code.";";
			
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result);
			return $row["cant"];
		}
		
		/**
		*	Función responsable de asociar el codigo de un pais de master-unlock con el de una API.
		*	@autor: JSL
		*	@date: 22/02/2012
		*/
		function update_API_country($data){
			$sql = "UPDATE api_country SET MU_country_code = '".$data->new_MU_country_code."' WHERE API_country_code=".$data->API_country_code.";";
			return mysql_query($sql);
		}
		
		/**
		*	Funcion responsable de obtener una lista de marcas que requiera un servicio de una api especifica y esten seleccionadas desde master-unlock.
		*	@autor: JSL
		*	@date: 24/02/2012
		*/
		function listBrandsSyncAPI($ser_code, $API_provider_code, $all = 0 ){
			switch ( $API_provider_code ){
				case 1://unlockbase
					if ( $all ){
						$sql = "
						SELECT b.bra_code, b.bra_name FROM brand b JOIN
						(SELECT m.bra_code FROM model m 
							JOIN
						(SELECT am.MU_mobile_code AS mod_code FROM api_mobile am WHERE am.API_provider_code=".$API_provider_code.") t
	
						ON m.mod_code=t.mod_code GROUP BY m.bra_code) t1
						ON b.bra_code=t1.bra_code ORDER BY b.bra_name;";
					}				
					else{
						$sql = "
						SELECT b.bra_code, b.bra_name FROM brand b JOIN
						(SELECT m.bra_code FROM model m 
							JOIN
						(SELECT t0.mod_code FROM
						(SELECT sm.mod_code FROM services_model sm WHERE sm.ser_code=".$ser_code.") t0
						JOIN
						( SELECT am.MU_mobile_code FROM api_mobile am WHERE am.API_provider_code=".$API_provider_code.") t1
						ON t0.mod_code=t1.MU_mobile_code) t
	
						ON m.mod_code=t.mod_code GROUP BY m.bra_code) t1
						ON b.bra_code=t1.bra_code ORDER BY b.bra_name;";
					}
				break;
				
				case 2://unlockcodesource
					if ( $all ){
						$sql = "
						SELECT b.bra_code, b.bra_name FROM brand b JOIN
						(SELECT ab.MU_brand_code AS bra_code FROM api_brand ab WHERE ab.API_provider_code=".$API_provider_code.") t
						ON b.bra_code=t.bra_code ORDER BY b.bra_name;";
					}				
					else{
						$sql = "
						SELECT b.bra_code, b.bra_name FROM brand b JOIN
						
						(SELECT t1.bra_code FROM
							(SELECT m.bra_code FROM
							(SELECT sm.mod_code FROM services_model sm WHERE sm.ser_code=".$ser_code.") t0
							JOIN
							model m
							ON t0.mod_code=m.mod_code GROUP BY m.bra_code) t1
						JOIN api_brand ab 
						ON t1.bra_code=ab.MU_brand_code WHERE ab.API_provider_code=".$API_provider_code.") t
						
						ON b.bra_code=t.bra_code ORDER BY b.bra_name;";
						//die($sql);
					}
					
					
				break;
				
				default:
					return NULL;
				break;
			}
			
			if ( $result = mysql_query ($sql) ){
				$i = 0;
				$data;
				
				while ( $row = mysql_fetch_array ($result) ){
					$data[$i]->bra_code = $row["bra_code"];
					$data[$i]->bra_name = $row["bra_name"];
					
					$i++;
				}
				
				return $data;
			}
			else
				return false;
		}
		
		/**
		*	Funcion responsable de obtener una lista de modelos que requiera un servicio de una api especifica y esten seleccionadas desde master-unlock.
		*	@autor: JSL
		*	@date: 27/02/2012
		*/
		function listModelsSyncAPI($API_provider_code, $ser_code, $bra_code){
			switch ( $API_provider_code ){
				case 1://unlockbase
				
						$sql = "
						SELECT t0.mod_code, t0.mod_name FROM
							(SELECT t1.mod_code, t1.mod_name FROM (SELECT sm.mod_code FROM services_model sm WHERE sm.ser_code=".$ser_code.") t
							JOIN (SELECT m.mod_code, m.mod_name FROM model m WHERE m.bra_code=".$bra_code.") t1
							ON t.mod_code=t1.mod_code) t0
						JOIN
						( SELECT am.MU_mobile_code FROM api_mobile am WHERE am.API_provider_code=".$API_provider_code.") t1
						ON t0.mod_code=t1.MU_mobile_code ORDER BY t0.mod_name;";
					
				break;
				
				case 2://unlockcodesource
						$sql_aux = "SELECT COUNT(*) AS cant FROM services_model sm JOIN model m ON sm.mod_code=m.mod_code WHERE sm.ser_code=".$ser_code.";";
						$cant = 0;
						if ( $result = mysql_query ($sql_aux) ){
							$row = mysql_fetch_array ($result);
							$cant = $row["cant"];
						}
						
						if ( $cant == 0 ){//requiere todas los modelos;
							$sql = "
								SELECT t.mod_code, t.bra_code, t.mod_name FROM
								(SELECT m.mod_code, m.bra_code, m.mod_name FROM model m WHERE m.bra_code=".$bra_code.") t
								JOIN api_brand ab ON t.bra_code=ab.MU_brand_code WHERE ab.API_provider_code=".$API_provider_code." ORDER BY t.mod_name;
							";
						}
						else{
							$sql = "
								SELECT t.mod_code, t.bra_code, t.mod_name FROM
									(SELECT t.mod_code, t.bra_code, t.mod_name FROM
									(SELECT m.mod_code, m.bra_code, m.mod_name FROM model m WHERE m.bra_code=".$bra_code.") t
									JOIN
									(SELECT sm.mod_code FROM services_model sm WHERE sm.ser_code=".$ser_code.") t1
									ON t.mod_code=t1.mod_code) t
								JOIN api_brand ab ON t.bra_code=ab.MU_brand_code WHERE ab.API_provider_code=".$API_provider_code." ORDER BY t.mod_name;;";
						}
				break;
				
				default:
					return NULL;
				break;
			}
			//die($sql);
			if ( $result = mysql_query ($sql) ){
				$i = 0;
				$data;
				
				while ( $row = mysql_fetch_array ($result) ){
					$data[$i]->mod_code = $row["mod_code"];
					$data[$i]->mod_name = $row["mod_name"];
					
					$i++;
				}
				
				return $data;
			}
			else
				return false;
		}
		
		
		/**
		*	Esta función es la encargada de obtener la informacion de una marca en Master-Unlock, para la API obtiene cod y nombre si el mobile tiene relacion alguna.
		*	@parameter: $bra_code	--> es el cod de la marca en master-unlock.
		*	@autor: JSLP.
		*	@date: 28 FEB 2011.
		*/
		public function getBrandMU($bra_code,$API_provider_code){
			$sql = "
			SELECT b.bra_code, b.bra_name, ab.API_provider_brand_code, ab.API_provider_brand_name FROM brand b
			LEFT JOIN api_brand ab ON b.bra_code=ab.MU_brand_code WHERE b.bra_code=".$bra_code." AND ab.API_provider_code=".$API_provider_code.";			
			";
			
			$result = mysql_query($sql);			
			$row=mysql_fetch_array($result);
			
			$data->bra_code = $row["bra_code"];
			$data->bra_name = $row["bra_name"];
			$data->API_provider_brand_code = $row["API_provider_brand_code"];
			$data->API_provider_brand_name = $row["API_provider_brand_name"];
			
			return $data;
		}
	}
?>